package com.lmx.tour_management_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmx.tour_management_system.mapper.AccountMapper;
import com.lmx.tour_management_system.pojo.Account;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lmx
 * @since 2021-01-11
 */
@Controller
public class AccountController {

    @Resource
    AccountMapper accountMapper;

    /**
     * 账号登录业务
     * @param username 登录用户名
     * @param password 登录密码
     * @param session 当前Session(一个会话窗口共享一个Session
     * @param model (返回model
     * @return
     */
    @RequestMapping("/account/login")
    public String login(String username, String password, HttpSession session, Model model){

        System.out.println("test=>>>AccountController.login"+username+password);

        //根据账号从数据库读取数据 封装到Account对象里
        Account account = accountMapper.selectOne(new QueryWrapper<Account>().eq("account_username", username));


        //如果账号不存在
        if(account==null){
            model.addAttribute("msg","账号不存在！");
            return "login";
        }else if(!account.getAccountPassword().equals(password)){
            //如果密码错误
            model.addAttribute("msg","密码错误!");
            return "login";
        }


        //登陆成功
        if(account.getAccountPassword().equals(password)){
            //如果登陆成功 往Session域里面放置loginUser标识
            session.setAttribute("loginUser", account);
            return "admin/main";
        }else {
            model.addAttribute("msg","系统错误!");
        }
        return "login";

    }


    @RequestMapping("/admin/account/add")
    public String addAccount(Account account,Model model){
        Account newAccount = new Account();
        newAccount.setAccountUsername(account.getAccountUsername());
        newAccount.setAccountPassword(account.getAccountPassword());

        System.out.println(newAccount);

        try {
            int insert = accountMapper.insert(newAccount);
            model.addAttribute("msg","添加成功!");
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("msg","用户名已被使用!");
        }

        return "admin/account_add";
    }



    @RequestMapping("/admin/account/delete/{id}")
    public String deleteAccount(@PathVariable("id") int id,HttpSession session){
        int result = accountMapper.delete(new QueryWrapper<Account>().eq("account_id",id));

        /**
         * 如果把自己的账号删掉了,那就移除当前Session踢出去
         */
        Account currentAccount = (Account) session.getAttribute("loginUser");
        if(id==currentAccount.getAccountId()){
            session.removeAttribute("loginUser");
        }

        return "redirect:/admin/toAccountList";
    }

    @RequestMapping("/admin/account/updatePassword")
    public String updatePassword(Account account,String checkPassword,Model model,HttpSession session){

        if(!account.getAccountPassword().equals(checkPassword)){
            model.addAttribute("msg","确认密码不一致!");
            return "admin/account_updatePassword";
        }

        int i = accountMapper.updateById(account);
        if(i==1){

            session.removeAttribute("loginUser");
        }else {
            model.addAttribute("msg","修改失败!");
        }
        model.addAttribute("msg","修改成功!请重新登录!");
        return "forward:/toLogin";

    }




}

